<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 邮箱设置
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use app\admin\validate\Sms as SmsValidate;
use app\admin\validate\Phone as PhoneValidate;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\facade\View;
use think\facade\Db;

class Sms extends Base
{
  // 路由列表
  public function index()
  {
    $data = Db::name('sms')->where('sms','sms')->find();
    View::assign('data',$data);
    return View::fetch();
  }

  // 修改短信配置
  public function smsPost()
  {
    if($this->request->isPost()) {
      $post = $this->request->post();
      //验证
      (new SmsValidate())->goCheck();
      if(false == Db::name('sms')->where('sms','sms')->update($post)) {
        return error('提交失败');
      } else {
        adminLog();//写入日志
        return success('提交成功','admin/sms/index');
      }
    } else {
      return error('非法请求');
    }
  }

  // 发送测试短信
  public function trySms()
  {
    if($this->request->isPost()) {
      $post = $this->request->post();
      //验证  
      (new PhoneValidate())->goCheck();
      $phone = (string)$post['phone'];
      $param = '{"name":"用户"}';
      $smsTo = self::SendSms($param,$phone);
      return error($smsTo);
      if(!empty($smsTo)) {
        return error('发送失败');
      } else {
        // $phone = hide_phone($phone); // 隐藏手机号中间四位
        adminLog();//写入日志
        return success('短信发送成功');
      }
    }
  }

  /**
   * 阿里大鱼短信发送
   * @param [type] $appkey    [description]
   * @param [type] $secretKey [description]
   * @param [type] $type      [description]
   * @param [type] $name      [description]
   * @param [type] $param     [description]
   * @param [type] $phone     [description]
   * @param [type] $code      [description]
   * @param [type] $data      [description]
   */
  private static function SendSms($param,$phone)
  {
    //获取短信配置
    $data = Db::name('sms')->where('sms','sms')->find();
    $appkey = $data['appkey'];
    $secretkey = $data['secretkey'];
    $type = $data['type'];
    $name = $data['name'];
    $code = $data['code'];

    AlibabaCloud::accessKeyClient($appkey, $secretkey)
      ->regionId('cn-beijing')
      ->asDefaultClient();

    try {
      $result = AlibabaCloud::rpc()
        ->product('Dysmsapi')
        // ->scheme('https') // https | http
        ->version('2017-05-25')
        ->action('QuerySendDetails')
        ->method('POST')
        ->host('dysmsapi.aliyuncs.com')
        ->options([
          'query' => [
            'PhoneNumber' => $phone,
            'SendDate' => date('Y-m-d'),
            'PageSize' => "1",
            'CurrentPage' => "1",
          ],
        ])
        ->request();
      return $result;
    } catch (ClientException $e) {
      return error($e->getErrorMessage() . PHP_EOL);
    } catch (ServerException $e) {
      return error($e->getErrorMessage() . PHP_EOL);
    }
  }

}

?>